package com.yxw.yx_hotel.exception;

import com.yxw.yxnet_cd_center.common.enums.CommonErrorEnum;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 集中处理所有异常
 * @author Administrator
 */
@Slf4j
@RestControllerAdvice(basePackages = "com.yxw.yx_hotel")
public class CSSExceptionControllerAdvice {

    /**
     * 字段空校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public BaseResult handleNotValidException(MethodArgumentNotValidException e) {
        Map<String, String> errorMap = new HashMap<>(16);
        BaseResult result = new BaseResult<>();
        result.setResult(false);
        result.setResultCode(CommonErrorEnum.PARAM_ERROR.getCode());
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        if(null!=allErrors && !allErrors.isEmpty()){
            StringBuffer sbf=new StringBuffer();
            for (int i = 0; i < allErrors.size(); i++) {
                ObjectError error = allErrors.get(i);
                sbf.append((i+1)+":"+error.getDefaultMessage()+"\r\n");
            }
            result.setResultMsg(sbf.toString());
        }else{
            result.setResultMsg("未知异常,请联系管理员处理");
        }
        result.setData(errorMap);
        return result;
    }

    /**
     * 处理Exception异常
     */
    @ExceptionHandler(value = Exception.class)
    public BaseResult exceptionHandler(Exception e) {
        StringBuffer stringBuffer = new StringBuffer("\n错误信息:=======================================>>>>>[" + e.toString()+"]\n");
        StackTraceElement[] stackTrace = e.getStackTrace();
        if(null!=stackTrace && stackTrace.length>0){
            for (int i=0;i<stackTrace.length;i++) {
                StackTraceElement element = stackTrace[i];
                stringBuffer.append("序号:[" + (i+1)+ "] " +
                        "文件:[" + element.getFileName() + "] " +
                        "方法:[" + element.getMethodName() + "] " +
                        "代码行:[" + element.getLineNumber() + "] \n");
            }
        }
        Map<String, String> errorMap = new HashMap<>(16);
        log.info(stringBuffer.toString());
        BaseResult result = new BaseResult<>();
        result.setResult(false);
        result.setData(errorMap);
        if(e instanceof BaseException){
            BaseException baseException = (BaseException) e;
            result.setResultMsg(baseException.getDescription());
            result.setResultCode(baseException.getCode());
        }else{
            result.setResultMsg(e.getMessage());
            result.setResultCode(CommonErrorEnum.UNKNOWN_ERROR.getCode());
        }
        return result;
    }
}